<?php
	DEFINE('H1', 'Try This 7-2: Adding Employees  to a Database');
	DEFINE('TITLE','Try This 7-2: Adding Employees  to a Database');
	
	$INCL1 = 'style/employee';
	global $INCL1;
	
	require('includes/header.php');
?>	
	<h2><?PHP echo H1 ?></h2>
	
<?php

if (isset($_POST['submit'])){
	require_once ('db/DBParms.php');
	$mysqli = new  mysqli(DBHOST, DBUSER, DBPWD, "employees");
	
	if($mysqli === false) {
		die("Error: Could not connect to database . " . mysqli_connect_error());
	}
	
	echo '<div id="message">';
	
	$inputError = false;
	if (empty($_POST['emp_name'])) {
		echo 'Error: Please enter a valid employee name';
		$inputError = true;
	} else {
		$name = $mysqli->escape_string($_POST['emp_name']);
	}
	
	if ($inputError != true && empty($_POST['emp_desig'])) {
		echo 'Error: Please enter a valid employee designation';
		$inputError = true;
	} else {
		$designation = $mysqli->escape_string($_POST['emp_desig']);
	}
	
	if ($inputError != true) {
		$sql = "INSERT INTO employees (name, designation, user) 
					VALUES ('$name', '$designation', '" . DBUSER . "')";
			if($mysqli->query($sql) === true) {
				echo 'New employee record added with ID: ' . $mysqli->insert_id;
			} else {
				echo "Error: Could not execute query: $sql. " . $mysqli->error;
			}
	}
	
	echo '</div>';
	$mysqli->close();
}
?>

<form action="employee.php" method="post">
	Employee name:<br />
	<input type="text" name="emp_name" size="40" />
	<br /><br />
	Employee designation:<br />
	<input type="text" name="emp_desig" size="40" />
	<br /><br />
	<input type="submit" name="submit" value="Submit" />
</form>

<?php
	require('includes/footer.php');
?>